﻿using MapleServer2.Data.Static;
using MapleServer2.Database;
using MapleServer2.Enums;
using MapleServer2.Packets;
using MapleServer2.Servers.Game;

namespace MapleServer2.Types;

public class Mail
{
    public long Id { get; set; }
    public MailType Type { get; set; }
    public long RecipientCharacterId { get; set; }
    public long SenderCharacterId { get; set; }
    public string SenderName { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public long ReadTimestamp { get; set; }
    public long SentTimestamp { get; set; }
    public long ExpiryTimestamp { get; set; }
    public long Mesos { get; set; }
    public long Merets { get; set; }
    public List<Item> Items = new();
    public string AdditionalParameter1 = "";
    public string AdditionalParameter2 = "";

    public Mail() { }

    public Mail(MailType type, long recipientCharacterId, long senderCharacterId, string senderName, string title, string body, string addParameter1, string addParameter2, List<Item> items, long mesos, long merets)
    {
        Type = type;
        RecipientCharacterId = recipientCharacterId;
        SenderCharacterId = senderCharacterId;
        SenderName = senderName;
        Title = title;
        Body = body;
        SentTimestamp = TimeInfo.Now();
        int sellEndtime = int.Parse(ConstantsMetadataStorage.GetConstant("MailExpiryDays")) * 86400;
        ExpiryTimestamp = SentTimestamp + sellEndtime;
        Items = items;
        Mesos = mesos;
        Merets = merets;
        AdditionalParameter1 = addParameter1;
        AdditionalParameter2 = addParameter2;
        Id = DatabaseManager.Mails.Insert(this);
        foreach (Item item in items)
        {
            item.MailId = Id;
            DatabaseManager.Items.Update(item);
        }
    }

    public Mail(long id, MailType type, long recipientCharacterId, long senderCharacterId, string senderName, string title, string body, long sentTimestamp, long expiryTimestamp, long readTimestamp, string addParameter1,
        string addParameter2, List<Item> items, long mesos, long merets)
    {
        Id = id;
        Type = type;
        RecipientCharacterId = recipientCharacterId;
        SenderCharacterId = senderCharacterId;
        SenderName = senderName;
        Title = title;
        Body = body;
        SentTimestamp = sentTimestamp;
        ReadTimestamp = readTimestamp;
        ExpiryTimestamp = expiryTimestamp;
        AdditionalParameter1 = addParameter1;
        AdditionalParameter2 = addParameter2;
        Items = items;
        Mesos = mesos;
        Merets = merets;
    }

    public void Read(GameSession session)
    {
        ReadTimestamp = TimeInfo.Now();
        DatabaseManager.Mails.Update(this);
        session.Send(MailPacket.Read(this));
    }

    public void Delete(GameSession session)
    {
        if (Items.Count > 0)
        {
            foreach (Item item in Items)
            {
                DatabaseManager.Items.Delete(item.Uid);
            }
        }

        session.Player.Mailbox.Remove(this);
        session.Send(MailPacket.Delete(this));
        DatabaseManager.Mails.Delete(Id);
    }
}
